import Vue from "vue";
import Vuex from "vuex";
import { Message } from "element-ui";
import { searchStaionList } from "../api/base8090.js";
import { login } from "../api/base8012.js";
import JSEncrypt from "jsencrypt";
import VuexPersist from "vuex-persist";
Vue.use(Vuex);
const vuexLocal = new VuexPersist({
  storage: window.sessionStorage,
});
export default new Vuex.Store({
  state: {
    showStList: false,
    isCenter: false,
    stationName: "左炮台东站",
    stationList: [],
    deptName: "",
    username: "",
    tabStatus: false,
    showLink: true,
    isLink: false,
    stationCode: "",
  },
  mutations: {
    SET_SHOW(state, val) {
      state.showStList = val;
    },
    SET_CENTER(state, val) {
      state.isCenter = val;
      sessionStorage.setItem("isCenter", val);
    },
    SET_STATIONNAME(state, val) {
      state.stationName = val;
    },
    SET_STATIONID(state, val) {
      state.stationCode = val;
      sessionStorage.setItem("stationCode", val);
    },
    SET_STATIONCODE(state, val) {
      state.stationCode = val;
      sessionStorage.setItem("stationCode", val);
    },
    SET_NAVLINK(state, val) {
      state.showLink = val;
      sessionStorage.setItem("showLink", val);
    },
    SET_TABSTATUS(state, val) {
      state.tabStatus = val;
      sessionStorage.setItem("tabStatus", val);
    },

    SET_STATIONLIST(state, val) {
      state.stationList = val;
      sessionStorage.setItem("stationList", JSON.stringify(val));
    },
    SET_DEPTNAME(state, val) {
      state.deptName = val;
      sessionStorage.setItem("deptName", val);
    },
    SET_USERNAME(state, val) {
      state.username = val;
      sessionStorage.setItem("username", val);
    },
    SET_ISLINK(state, val) {
      state.isLink = val;
    },
  },
  actions: {
    //获取车站列表
    getStationList({ commit }) {
      return new Promise((resolve, reject) => {
        searchStaionList()
          .then((response) => {
            const data = response.data;
            commit("SET_STATIONLIST", data);
            resolve(response.data);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    handleLogin({ commit }, userInfo) {
      const Encry = new JSEncrypt();
      const publicKey =
        "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn0wChlUP05g7gBc31xml8lM3wyCsbLL7PEl/aC86IaMGv19w+D4+04RwHwyoFxK7A2xGaX8gR5CZ8eZjnn8UJySgKN6p18Zko/n2S3PhjlOt/aya/HHZvl+pAi82hJnq2VDMuTN4weLmhL108N4zj+aHLt6YnJuNn/xwfuYvCvb0j5tikeRrO+MIMc488ndDXSGA3BLK2choSOhRpsfCs8DGQPJLKgndPjitpaTkDjuGhFE/W0yPhVPx9CYtT7L/TiVkyha+ut+b0mkgvD8QLBaP530BiF78r3Fb4c4WfwUu5hn2i1IPcuEAeql+xoV5MNhqVteRUJzFmQ438OcUCQIDAQAB";
      Encry.setPublicKey(publicKey);

      const obj = {
        userName: userInfo.userName,
        passWord: Encry.encrypt(userInfo.passWord),
        systemCode: "iot_zhny_001",
        enterpriseCode: "iot-12",
      };

      login(obj).then((res) => {
        if (res.code == 0) {
          const role = res.result.additionalInformation.isCenter;
          const deptCode = res.result.additionalInformation.deptCode;
          const deptName = res.result.additionalInformation.deptName;
          const username = res.result.additionalInformation.username;

          const lineToken = "Bearer" + res.result.value;
          sessionStorage.setItem("token_12", lineToken);
          sessionStorage.setItem("timestamp_12", new Date().getTime());
          checkRole();
          function checkRole() {
            if (role) {
              sessionStorage.setItem("stationCode", "S00");
              commit("SET_CENTER", true);
              commit("SET_NAVLINK", true);
              commit("SET_STATIONCODE", "S00");
              commit("SET_USERNAME", username);
              commit("SET_STATIONID", "S00");
            } else {
              commit("SET_CENTER", false);
              commit("SET_NAVLINK", false);
              commit("SET_STATIONCODE", deptCode);
              commit("SET_DEPTNAME", deptName);
              commit("SET_USERNAME", username);
              commit("SET_STATIONID", deptCode);
            }
          }
          Message.success({
            message: "欢迎登入！",
            duration: 500,
          });
        } else {
          Message.error(res.message);
          return false;
        }
      });
    },
  },
  modules: {},
  plugins: [vuexLocal.plugin],
});
